﻿
// #define TEST

using System;
using System.Net;
using System.Text;

namespace UdpPublisher
{
#if TEST
    class Tester
    {
        private UdpPublisher m_publisher = new UdpPublisher(6001);

        void TestSubscriberChange()
        {
            m_publisher.SubscriberAdded += OnSubscriberAdded;
            m_publisher.SubscriberRemoved += OnSubscriberRemoved;
            m_publisher.Start();

            Console.ReadLine();

            m_publisher.Dispose();
        }

        private void OnSubscriberAdded(object source,SubscriberChangeEvtArgs evtargs)
        {
            Console.WriteLine("[NEW SUBSCRIBER]: {0}",evtargs.ChangedSubscriber.ToString());
            ShowSubscribers((UdpPublisher)source);
        }

        private void OnSubscriberRemoved(object source,SubscriberChangeEvtArgs evtargs)
        {
            Console.WriteLine("[REMOVED SUBSCRIBER]: {0}",evtargs.ChangedSubscriber.ToString());
            ShowSubscribers((UdpPublisher)source);
        }

        /// <summary>
        /// this method will be invoked within the critical section, so no synchronization need
        /// </summary>
        private void ShowSubscribers(UdpPublisher svr)
        {
            Console.WriteLine("******** current {0} subscribers ***********",svr.NumOfSubscribers);

            int index = 1;
            foreach (IPEndPoint subEndpoint in svr)
            {
                Console.WriteLine("[{0}] {1}",index,subEndpoint.ToString());
                ++index;
            }
        }

        static void Main(string[] args)
        {
            Tester tester = new Tester();
            tester.TestSubscriberChange();
        }
    }
#else
    class GreetingGenerator
    {
        private byte[] m_greetBytes;

        public GreetingGenerator(string greetString)
        {
            m_greetBytes = Encoding.ASCII.GetBytes(greetString);
        }

        public byte[] GetGreetBytes() { return m_greetBytes; }
    }

    class CurrentTimeGenerator
    {
        public static byte[] GetTimeBytes()
        {
            return Encoding.ASCII.GetBytes(DateTime.Now.ToShortTimeString());
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            UdpPublisher publisher = new UdpPublisher(6001);
            publisher.SubscriberAdded += OnSubscriberAdded;
            publisher.SubscriberRemoved += OnSubscriberRemoved;

            //GreetingGenerator generator = new GreetingGenerator("Hello From WSU");
            //publisher.PubGenerator = new TimerPublisher(1000, generator.GetGreetBytes);
            publisher.PubGenerator = new TimerPublisher(2000,CurrentTimeGenerator.GetTimeBytes);

            publisher.Start();
            Console.ReadLine();
            publisher.Dispose();
        }

        static private void OnSubscriberAdded(object source, SubscriberChangeEvtArgs evtargs)
        {
            Console.WriteLine("[NEW SUBSCRIBER]: {0}", evtargs.ChangedSubscriber.ToString());
        }

        static private void OnSubscriberRemoved(object source, SubscriberChangeEvtArgs evtargs)
        {
            Console.WriteLine("[REMOVED SUBSCRIBER]: {0}", evtargs.ChangedSubscriber.ToString());
        }
    }
#endif
}
